﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;

public partial class DayToDayManagement_CMDDOWNEDIT : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }


    private bool IsUseColorSize
    {
        get
        {
            return new BLL.SYSPARAM().GetProductUpDownColorSizeConfig();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300100104");
        if (!IsPostBack)
        {
            uptype();
            Bind();
        }
    }

    private void uptype()
    {
        List<Model.DOWNTYPE> properties = new BLL.DOWNTYPE().GetAllList();
        dropDownType.DataSource = properties;
        dropDownType.DataTextField = "DT_NAME";
        dropDownType.DataValueField = "DT_CODE";
        dropDownType.DataBind();
    }

    private void Bind()
    {
        if (Request.QueryString["code"] == null)
        {
            txtDownOperator.ToolTip = Share.UserValue(0);
            txtDownOperator.Text = Share.UserValue(1);
            txtDownDate.Text = DateTime.Now.ToShortDateString();
            txtCode.Text = BLL.Common.GetDM("300100104");
            txtDownOperator.Enabled = false;
            txtDownDate.Enabled = false;
            txtAuditor.Enabled = false;
            txtAuditDate.Enabled = false;
            txtCode.Enabled = txtCode.Text.Trim() == "" ? true : false;
            ((Button)BUTTONDJ1.Controls[2]).Visible = false;
            ((Button)BUTTONDJ1.Controls[0]).Visible = false;
            ((Button)BUTTONDJ1.Controls[6]).Enabled = false;
            ((Button)BUTTONDJ1.Controls[8]).Visible = false;
            ((Button)BUTTONDJ1.Controls[10]).Visible = false;
            ((Button)BUTTONDJ1.Controls[12]).Visible = false;
            ((Button)BUTTONDJ1.Controls[14]).Visible = false;
            ((Button)BUTTONDJ1.Controls[16]).Visible = false;
            ((Button)BUTTONDJ1.Controls[18]).Visible = false;
            ((Button)BUTTONDJ1.Controls[20]).Visible = false;
        }
        else
        {
            if (Request.QueryString["code"].ToString().Trim() != "")
            {
                Label1.Text = "编辑商品下架单";
                Model.CMDDOWN cmd = new BLL.CMDDOWN().GetModel(Request.QueryString["code"].ToString().Trim());
                dropDownType.SelectedValue = cmd.CD_DOWNTYPE;
                txtDownOperator.ToolTip = cmd.CD_DOWNER;
                txtDownOperator.Text = cmd.CD_DOWNER;
                txtDownDate.Text = cmd.CD_DOWNDATE.ToShortDateString();
                txtAuditor.Text = cmd.CD_CHECKER;
                txtAuditDate.Text = cmd.CD_CHECKDATE == DateTime.MinValue ? "" : cmd.CD_CHECKDATE.ToShortDateString();
                txtNote.Text = cmd.CD_NOTE;
                txtCode.Text = Request.QueryString["code"].ToString().Trim();
                txtDownOperator.Enabled = false;
                txtDownDate.Enabled = false;
                txtAuditor.Enabled = false;
                txtAuditDate.Enabled = false;
                txtCode.Enabled = false;

                if (IsUseColorSize)
                {
                    DataSet ds = new BLL.CMDDOWNDETAIL().GetCMDDOWNDETAIL(Request.QueryString["code"].ToString().Trim());
                    ListItem item;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        item = new ListItem();
                        item.Value = ds.Tables[0].Rows[i]["spdm"].ToString() + "~" + ds.Tables[0].Rows[i]["colorCode"].ToString() + "~" + ds.Tables[0].Rows[i]["sizeCode"].ToString();
                        item.Text = ds.Tables[0].Rows[i]["spdm"].ToString() + " " + ds.Tables[0].Rows[i]["spmc"].ToString() + " " + ds.Tables[0].Rows[i]["colorName"].ToString() + " " + ds.Tables[0].Rows[i]["sizeName"].ToString();
                        Select1.Items.Add(item);
                    }
                }
                else
                {
                    DataSet ds = new BLL.CMDDOWNDETAIL().GetCMDDOWNDETAILWithoutColorSize(Request.QueryString["code"].ToString().Trim());
                    ListItem item;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        item = new ListItem();
                        item.Value = ds.Tables[0].Rows[i]["spdm"].ToString();
                        item.Text = ds.Tables[0].Rows[i]["spdm"].ToString() + " " + ds.Tables[0].Rows[i]["spmc"].ToString();
                        Select1.Items.Add(item);
                    }
                }


                ((Button)BUTTONDJ1.Controls[2]).Visible = false;
                //((Button)BUTTONDJ1.Controls[4]).Visible = false;
                ((Button)BUTTONDJ1.Controls[8]).Visible = false;
                ((Button)BUTTONDJ1.Controls[10]).Visible = false;
                ((Button)BUTTONDJ1.Controls[12]).Visible = false;
                ((Button)BUTTONDJ1.Controls[14]).Visible = false;
                ((Button)BUTTONDJ1.Controls[16]).Visible = false;
                ((Button)BUTTONDJ1.Controls[18]).Visible = false;
                ((Button)BUTTONDJ1.Controls[20]).Visible = false;
                ((Button)BUTTONDJ1.Controls[0]).Visible = false;
                if (cmd.CD_CHECKER.Trim() != "")
                {
                    ((Button)BUTTONDJ1.Controls[4]).Enabled = false;
                    ((Button)BUTTONDJ1.Controls[6]).Enabled = false;
                }
            }
        }
    }

    public void Save()
    {
        try
        {
            Model.CMDDOWN cmd = new Model.CMDDOWN();
            cmd.CD_ID = txtCode.Text.Trim();
            cmd.CD_DOWNTYPE = dropDownType.SelectedValue;
            cmd.CD_DOWNER = txtDownOperator.ToolTip;
            cmd.CD_DOWNDATE = DateTime.Parse(txtDownDate.Text.Trim());
            cmd.CD_NOTE = txtNote.Text;
            cmd.CD_CHECKER = txtAuditor.Text;
            cmd.CD_CHECKDATE = txtAuditDate.Text.Trim() == "" ? DateTime.MinValue : DateTime.Parse(txtAuditDate.Text.Trim());
            if (Hidden1.Value.Trim().Length > 1)
            {
                string spdmstr = Hidden1.Value.Trim();

                if (Request.QueryString["code"] == null)
                {
                    if (!Check.CheckPermission("300100104", "00001"))
                    {
                        Share.Alert(Page, "无权操作"); return;
                    }
                    int val = new BLL.CMDDOWN().P_CMDDOWN_Save(0, cmd.CD_ID, cmd.CD_DOWNTYPE, cmd.CD_DOWNER, cmd.CD_DOWNDATE, cmd.CD_NOTE, cmd.CD_CHECKER, cmd.CD_CHECKDATE, spdmstr);
                    if (val == 1)
                    {
                        BLL.Common.ADDLSH("300100104");
                        Share.InsertLog("300100104", "00001");
                    }
                    else if (val == -2)
                    {
                        Share.Alert(Page, "编号已存在"); return;
                    }
                    else
                    {
                        Share.Alert(Page, "操作失败"); return;
                    }
                }
                else
                {
                    if (Request.QueryString["code"].ToString().Trim() != "")
                    {
                        if (!Check.CheckPermission("300100104", "00002"))
                        {
                            Share.Alert(Page, "无权操作"); return;
                        }
                        Model.CMDDOWN cmddown = new BLL.CMDDOWN().GetModel(Request.QueryString["code"].ToString().Trim());
                        if (cmddown != null)
                        {
                            if (cmddown.CD_CHECK == 1)
                            {
                                Share.Alert(Page, "此单据已被审核，不允许再次操作！"); return;
                            }
                        }
                        int val = new BLL.CMDDOWN().P_CMDDOWN_Save(1, cmd.CD_ID, cmd.CD_DOWNTYPE, cmd.CD_DOWNER, cmd.CD_DOWNDATE, cmd.CD_NOTE, cmd.CD_CHECKER, cmd.CD_CHECKDATE, spdmstr);
                        if (val == 1)
                        {
                            Share.InsertLog("300100104", "00002");
                        }
                        else
                        {
                            Share.Alert(Page, "操作失败"); return;
                        }
                    }
                }
                Response.Redirect("CMDDOWNEDIT.aspx?code=" + cmd.CD_ID);
            }
            else
            {
                Share.Alert(Page, "请选择商品"); return;
            }
        }
        catch (Exception ex)
        {
            Share.Alert(Page, ex.Message); return;
        }

    }

    public void Audit()
    {
        if (!Check.CheckPermission("300100104", "00010"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        Model.CMDDOWN cmddown = new BLL.CMDDOWN().GetModel(Request.QueryString["code"].ToString().Trim());
        if (cmddown != null)
        {
            if (cmddown.CD_CHECK == 1)
            {
                Share.Alert(Page, "此单据已被审核，不允许再次操作！"); return;
            }
        }
        if (new BLL.CMDDOWN().P_CMDDOWN_Audit(Request.QueryString["code"].ToString().Trim(), Share.UserValue(1)))
        {
            Share.InsertLog("300100104", "00010");
            Response.Redirect("CMDDOWNEDIT.aspx?code=" + Request.QueryString["code"].ToString().Trim());
        }
        else
        {
            Share.Alert(Page, "操作失败");
        }
    }
}
